package model.filemanager;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

public class MediaList {
	
	// MediaLists use 'user' as a property. Implementation is potentially insecure!
	
	//static
	private static DatastoreService datastore;
	
	static{
		datastore = DatastoreServiceFactory.getDatastoreService();
	}
	
	
	// Single list manipulation
	
	public static void addMedia(long listKey, String mediaKey){
		Entity ent =  new Entity("MediaListItems", mediaKey);
		ent.setProperty("listkey", listKey);
		ent.setProperty("mediakey", mediaKey); 
		// may also want to store order
		datastore.put(ent);
	}
	
	public static void removeMedia(long listKey, String mediaKey){ //removes a file from the list
		Entity ent = getMediaEntity(listKey, mediaKey);
		if(ent == null)
			return;
		datastore.delete(ent.getKey());
	}
	
	public static Entity getMediaEntity(long listKey, String mediakey){
		Query query = new Query("MediaListItems");
		query.addFilter("listkey", Query.FilterOperator.EQUAL, listKey);
		query.addFilter("mediakey", Query.FilterOperator.EQUAL, mediakey);
		
		List<Entity> results = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
		
		if(results != null) if(results.size()>0)
			return results.get(0);
		
		return null;
	}
	
	public static List<Entity> getMedia(long listKey){
		Query query = new Query("MediaListItems");
		query.addFilter("listkey", Query.FilterOperator.EQUAL, listKey);

		PreparedQuery pq = datastore.prepare(query);
		
		return pq.asList(FetchOptions.Builder.withDefaults());
		
	}
	
	
	
	// Lists manipulation 
	
	public static long AddList(String name, String type, String user){
		Entity ent =  new Entity("MediaList");
		ent.setProperty("name", name);
		ent.setProperty("type",type);
		ent.setProperty("user",user);
		long entKey = datastore.put(ent).getId();
		
		return entKey;
	}
	
	public static void RemoveList(String name, String type, String user){
		
		Entity ent = getListEntity(name, type, user);
		if(ent != null){
			datastore.delete(ent.getKey());
		}
	}
	
	public static void RemoveList(long key){
		
		if(key != -1){
			datastore.delete(KeyFactory.createKey("MediaList", key));
		}
	}
	
	public static long getListKey(String name, String type, String user){
		Entity ent = getListEntity(name, type, user);
		if(ent != null){
			return ent.getKey().getId();
		}
		return -1;
	}
	
	public static List<Entity> getLists(String type, String user){
		Query query = new Query("MediaList");
		query.addFilter("type", Query.FilterOperator.EQUAL, type);
		query.addFilter("user", Query.FilterOperator.EQUAL, user);

		PreparedQuery pq = datastore.prepare(query);
		
		return pq.asList(FetchOptions.Builder.withDefaults());
	}
	
	
	
	
	public static Entity getListEntity(String name, String type, String user){
		Query query = new Query("MediaList");
		query.addFilter("name", Query.FilterOperator.EQUAL, name);
		query.addFilter("type", Query.FilterOperator.EQUAL, type);
		query.addFilter("user", Query.FilterOperator.EQUAL, user);

		PreparedQuery pq = datastore.prepare(query);
		
		List<Entity> results = pq.asList(FetchOptions.Builder.withDefaults());
		if(results != null) if(results.size()>0)
			return results.get(0);
		
		return null;
	}
	
	public static Entity getListEntity(long listKey){
		if(listKey == -1)
			return null;
		try {
			return datastore.get(KeyFactory.createKey("MediaList", listKey));
		} catch (EntityNotFoundException e) {
			
		}
		
		return null;
	}
}
